//
//  PageMenuItemCornerRadiusButton.swift
//  Pods
//
//  Created by HOA on 2020/12/29.
//

import UIKit
import QuartzCore

class PageMenuItemCornerRadiusButton: PageMenuItem {
    
    public required init(coder  aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public required init(frame: CGRect, title: String, design: PageMenuItemDesign) {
        super.init(frame: frame, title: title, design: design)
        
        self.style = .cornerRadiusButton
        
        self.backgroundColor = design.backgroundColor
    }
    
    override func render(active: Bool) {
        if active {
            self.label.textColor = self.design.selectedColor
            self.label.font = self.design.selectedFont
            self.backgroundColor = self.design.selectedBackgroundColor
        }
        else {
            
            self.label.textColor = self.design.color
            self.label.font = self.design.font
            self.backgroundColor = self.design.backgroundColor
        }
        self.roundCorners(of: self)
    }
    
    
    func roundCorners(of label: UIView) {
        autoreleasepool {
            let radius: CGFloat = 15
            let bounds: CGRect = label.bounds.insetBy(dx: 0, dy: 0)
            let maskPath: UIBezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [ .allCorners ], cornerRadii: CGSize(width: radius, height: radius))
            let maskLayer: CAShapeLayer = CAShapeLayer()
            maskLayer.frame  = label.bounds
            maskLayer.path   = maskPath.cgPath
            label.layer.mask = maskLayer
            print(label)
        }
    }
}
